 <script type="text/javascript"><!--//--><![CDATA[//><!--
var LOVIsp_Produk = function(ao_option){
	try{
		if(ao_option==undefined){ ao_option={}; }
		var lf_onSelect	= (ao_option.onSelect==undefined || typeof(ao_option.onSelect)!='function') ? function(){} : ao_option.onSelect;
		
		// Create Popup
		var $popup	= openPopup({ title:'Daftar Produk' , width:700, height:370 });
		var ls_id	= $popup.attr('id');
		
		// Append Datagrid Container
		var ls_gridName	= 'grid_'+ls_id;
		$popup	.html(	$('<div>')	.attr('id', 'tb'+ls_id).attr('style','padding:5px;height:auto;')
									.html(	$('<div>').html($('<form>')	.attr('id','fm_search_'+ls_id).data('rel',ls_id).submit(function(e){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).data('rel')).serializeObject()); e.preventDefault(); })
																		.html('<table class="tb_form" style="margin-bottom:0px"><tr><td width="100">Keyword</td><td><input type="text" name="keyword" class="textfield medium" /><a href="#" class="easyui-linkbutton" iconCls="icon-search" id="btn_search'+ls_id+'" rel="'+ls_id+'">Search</a></td></tr></table>')
															)
											)
						)
				.append($('<div>').attr('id', ls_gridName).attr('toolbar', '#tb'+$popup.attr('id')));
		
		$('#btn_search'+ls_id).click(function(){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).attr('rel')).serializeObject()); return false; });
		$('#btn_search'+ls_id).linkbutton({  
			plain:true  
		}); 
		
		// Build Datagrid
		var ls_param = (ao_option.param==undefined ? '' : ao_option.param);
		$('#'+ls_gridName).datagrid({  
			url:gs_path+'/isp_pembelian_produk/getdata2/'+ls_param,
			queryParams: $('#fm_search_'+ls_id).serializeObject(),
			height:337,
			pagination:true,
			singleSelect:true,
			columns:[[
				{ field : 'id_inventori', title : 'ID Produk', width : 120, sortable:true },
				{ field : 'kode_inventori', title : 'Kode Produk', width : 120, sortable:true },
				{ field : 'nama_inventori', title : 'Nama Produk', width : 170, sortable:true },
				{ field : 'banyak', title : 'Stock', width : 100, sortable:true },
				{ field : 'harga', title : 'Harga Satuan', width : 170, sortable:true },
			]],
			onSelect:function(rowIndex, rowData){ lf_onSelect(rowData); closePopup(); }
		});
		
	} catch (err) { alert('Func. LOVSampleReference :\n' + err); }
};

var LOVIsp_Customer6 = function(ao_option){
	try{
		if(ao_option==undefined){ ao_option={}; }
		var lf_onSelect	= (ao_option.onSelect==undefined || typeof(ao_option.onSelect)!='function') ? function(){} : ao_option.onSelect;
		
		// Create Popup
		var $popup	= openPopup({ title:'Daftar Produk' , width:700, height:370 });
		var ls_id	= $popup.attr('id');
		
		// Append Datagrid Container
		var ls_gridName	= 'grid_'+ls_id;
		$popup	.html(	$('<div>')	.attr('id', 'tb'+ls_id).attr('style','padding:5px;height:auto;')
									.html(	$('<div>').html($('<form>')	.attr('id','fm_search_'+ls_id).data('rel',ls_id).submit(function(e){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).data('rel')).serializeObject()); e.preventDefault(); })
																		.html('<table class="tb_form" style="margin-bottom:0px"><tr><td width="100">Keyword</td><td><input type="text" name="keyword" class="textfield medium" /><a href="#" class="easyui-linkbutton" iconCls="icon-search" id="btn_search'+ls_id+'" rel="'+ls_id+'">Search</a></td></tr></table>')
															)
											)
						)
				.append($('<div>').attr('id', ls_gridName).attr('toolbar', '#tb'+$popup.attr('id')));
		
		$('#btn_search'+ls_id).click(function(){ $('#'+ls_gridName).datagrid('reload', $('#fm_search_'+$(this).attr('rel')).serializeObject()); return false; });
		$('#btn_search'+ls_id).linkbutton({  
			plain:true  
		}); 
		
		// Build Datagrid
		var ls_param = (ao_option.param==undefined ? '' : ao_option.param);
		$('#'+ls_gridName).datagrid({  
			url:gs_path+'/isp_pembelian_produk/getdata3/'+ls_param,
			queryParams: $('#fm_search_'+ls_id).serializeObject(),
			height:337,
			pagination:true,
			singleSelect:true,
			columns:[[
				{ field : 'id_customer', title : 'ID Client', width : 120, sortable:true },
				{ field : 'kode_customer', title : 'Kode Client', width : 120, sortable:true },
				{ field : 'nama', title : 'Nama Client', width : 200, sortable:true },
				{ field : 'nama_perusahaan', title : 'Nama Perusahaan', width : 200, sortable:true },
			]],
			onSelect:function(rowIndex, rowData){ lf_onSelect(rowData); closePopup(); }
		});
		
	} catch (err) { alert('Func. LOVSampleReference :\n' + err); }
};

	var grid_reload = function(){
		$('#datagrid').datagrid('reload', $('#fm_search').serializeObject());
	};

	$(document).ready(function() {

		// Setup Datagrid
		$('#datagrid').datagrid({  
			url:gs_path+'/'+controller+'/getdata', 
			queryParams: $('#fm_search').serializeObject(),
			height:340,
			pagination:true,
			columns:[[
				{ field : 'ck', checkbox:true, width: 40, align: 'center' },
				{ field : 'id_pembelian', title : 'ID Pembelian', width : 120, sortable:true },
				{ field : 'kode_customer', title : 'Kode Client', width : 120, sortable:true },
				{ field : 'nama', title : 'Nama Client', width : 200, sortable:true },
				{ field : 'nama_perusahaan', title : 'Nama Perusahaan', width : 200, sortable:true },
				{ field : 'kode_inventori', title : 'Kode Produk', width : 100, sortable:true },
				{ field : 'nama_inventori', title : 'Nama Produk', width : 150, sortable:true },
				{ field : 'banyak', title : 'Stock', width : 100, sortable:true },
				{ field : 'harga', title : 'Harga Satuan', width : 130, sortable:true },
				{ field : 'jumlah', title : 'Jumlah', width : 100, sortable:true },
				{ field : 'total_bayar', title : 'Total Harga', width : 130, sortable:true },
			]]
		});		


		
		// Setup Form
		$('#form_master').setupForm([			
			{ name: 'kode_customer', type: 'lov', size: 'short',
				onClearButton:function(){
					$(this).setval(''); 
					$('#nama').setval(''); 
					$('#nama_perusahaan').setval(''); 
				},
				onActionButton:function(){ 
					LOVIsp_Customer6({ onSelect:function(row){
						$('#id_customer').setval(row.id_customer);
						$('#kode_customer').setval(row.kode_customer); 
						$('#nama').setval(row.nama); 
						$('#nama_perusahaan').setval(row.nama_perusahaan); 
					} }); 
				}
			},
			{ name: 'nama', type: 'text', size: '35' },
			{ name: 'nama_perusahaan', type: 'text', size: '35' },
			{ name: 'kode_inventori', type: 'lov', size: 'short',
				onClearButton:function(){
					$(this).setval(''); 
					$('#nama_inventori').setval(''); 
					$('#banyak').setval(''); 
					$('#harga').setval(''); 
				},
				onActionButton:function(){ 
					LOVIsp_Produk({ onSelect:function(row){ 
						$('#id_inventori').setval(row.id_inventori);
						$('#kode_inventori').setval(row.kode_inventori); 
						$('#nama_inventori').setval(row.nama_inventori); 
						$('#banyak').setval(row.banyak); 
						$('#harga').setval(row.harga); 
					} }); 
				}
			},
			{ name: 'nama_inventori', type: 'text', size:'35' },
			{ name: 'banyak', type: 'text', size:'short'},
			{ name: 'harga', type: 'text', size:'medium'},
			{ name: 'jumlah', type: 'text', size:'short'},
			{ name: 'total_bayar', type: 'text', size:'medium'},
			//{ name: 'id_pembelian', type: 'text', size:'medium'}
		]);
		




		
		// Create Dialog
		$('#dialog_container').dialog({
			closed:true,
			modal:true,
			title: '_',
			width:600,
			height: 500,
			buttons:[{
				text:'Save',
				iconCls:'icon-save',
				handler:function(){
					$('#form_master').submitForm('/'+controller+'/ajax_post', { onComplete : function(){ $('#dialog_container').dialog('close'); $('#datagrid').datagrid('reload'); }} );
					//loadPage(gs_path + '/' + controller + '/index/');
				}
			},{
				text:'Close',
				iconCls:'icon-cancel',
				handler:function(){
					$('#dialog_container').dialog('close');
				}
			}]
		});
		
		
		// Set Action Button
		var buttons = [ { label: 'Add', icon: 'add', onclick: function() {
							
							resetError();
							/*$.ajax({
								url: "<?php echo base_url('isp_pembelian_produk/LastID'); ?>"
							}).done(function(value) {
								$('#id_pembelian').val(value);
							});*/

 							$.ajax({
								url:gs_path+'/'+controller+'/id_auto',
								type:'POST',
								dataType:'JSON',
								}).done(function(data){
									$('#id_pembelian').setval(data['id_pembelian_auto']);
							});

							$('#dialog_container').dialog('open');
							$('#form_master').data('mode', 'create');
							$('#form_master').parent().parent().parent().prev().find('.panel-title').html('New Penjualan Produk');
							$('#kode_customer, #nama, #nama_perusahaan, #kode_inventori, #nama_inventori, #banyak, #harga, #jumlah, #total_bayar').val('');
							$('#kode_customer').focus();
							$('#id_pembelian, #nama, #nama_perusahaan, #nama_inventori, #banyak, #harga, #total_bayar').readonly(true);

							//Calculation Keyup Function
								$('#jumlah').keyup(function() {
									var harga_satuan =  parseInt($('#harga').val());
									var jumlah =  parseInt($('#jumlah').val());
									var sub_total = harga_satuan * jumlah;
									$('#total_bayar').setval(sub_total);

									var banyak = parseInt($('#banyak').val());
	                                var j = banyak + 1;
	                                //alert(banyak);
	                                if(jumlah>=j){
	                                    alert(ls_eror ='input jumlah melebihi stock !\n');
	                                    $('#jumlah').setval('0');
	                                    $('#jum_akhir').setval('');
	                                } else {
	                                    var jum_akhir = banyak - jumlah;
	                                    
	                                    $('#update_stock').setval(jum_akhir);

	                                }
								});
						} },
						{ label: 'Edit', icon: 'edit', onclick: function() {
							var oSel=$('#datagrid').datagrid('getSelected'); 
							if(oSel==null){ alert('Please select row!'); }
							else{ 
								resetError();
								$('#dialog_container').dialog('open'); 
								$('#form_master').data('mode', 'edit');
								$('#form_master').parent().parent().parent().prev().find('.panel-title').html('Edit Penjualan Produk');
								$('#kode_customer, #id_pembelian,  #banyak, #harga, #total_bayar').readonly(true);
								jsonToForm(oSel); $('#kode_produk').focus();

								//Calculation Keyup Function
									$('#jumlah').keyup(function() {
										var jum_awal =  parseInt(oSel['jumlah']);
										var jumlah =  parseInt($('#jumlah').val());
										var banyak = parseInt($('#banyak').val());
										var harga_satuan =  parseInt($('#harga').val());
										$('#id_pembelian').setval(oSel['id_pembelian']);
										var ls_eror = '';
										//alert(jum_awal);
										$('#jum_awal').setval(jum_awal);
											var jum_akhir = jum_awal - jumlah;
											$('#jum_akhir').setval(jum_akhir);	

											if(jum_awal<=jumlah){
												var jum_cal = jum_akhir *= (-1) ;
												//alert(jum_akhir);
												var j = banyak + 1;
												$('#jum_akhir').setval(jum_cal);
													if(j<=jum_cal){
														alert(ls_eror ='input jumlah melebihi stock !\n');
														$('#jumlah').setval(jum_awal);
														$('#jum_akhir').setval('');
													} else {
														var update_stock = banyak - jum_cal;
													}
												$('#update_stock').setval(update_stock);
												
												var total_bayar = harga_satuan * jumlah;
												$('#total_bayar').setval(total_bayar);
											} else if(jum_awal>=jumlah) {
												//alert(jum_akhir);
												$('#jum_akhir').setval(jum_akhir);
												var update_stock = banyak + jum_akhir;
												$('#update_stock').setval(update_stock);

												var total_bayar = harga_satuan * jumlah;
												$('#total_bayar').setval(total_bayar); 
											}
									});
							} 
						} },
						{label: 'Delete', icon: 'delete', onclick: function() { 
							var oSel = $('#datagrid').datagrid('getSelected'); 
							if (oSel == null) { 
								$.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); 
							} else { 
								deleteData(['id_pembelian']);
							//loadPage(gs_path + '/' + controller + '/index/');
							} 
						}}
						];
		setAction(buttons);
		
		
		$('#fm_search').focusFirst();
	});
//--><!]]></script>

<div id="tb" style="padding:5px;height:auto">
    <div>
        <form id="fm_search" onsubmit="grid_reload(); return false;">
        	<table class="tb_form" style="margin-bottom:0px">
            <tr>
            	<td width="100">Keyword</td>
                <td><input type="text" id="keyword" name="keyword" class="textfield medium" />
                	<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="grid_reload(); return false;">Search</a>
                </td>
            </tr>
            </table>
        </form>
    </div>
</div>
<div id="datagrid" toolbar="#tb"></div>



<div id="dialog_container" style="padding:5px;width:600px;height:380px;">
	<form id="form_master">
    <table class="tb_form" style="width:100%;" cellpadding="0" cellspacing="0">
    <tr>
        <td>
        	<input id="id_pembelian" name="id_pembelian" type="hidden" />
        	<input id="id_inventori" name="id_inventori" type="hidden" />
        	<input id="id_customer" name="id_customer" type="hidden" />
        </td>
    </tr>
    <tr>
        <td class="label">Kode Customer</td>
        <td><input id="kode_customer" /></td>
    </tr>
    <tr>
        <td class="label">Nama Customer</td>
        <td><input id="nama" /></td>
    </tr>
    <tr>
        <td class="label">Nama Perusahaan</td>
        <td><input id="nama_perusahaan" /></td>
    </tr>
    <tr>
        <td class="label">Kode Produk</td>
        <td><input id="kode_inventori" /></td>
    </tr>
    <tr>
        <td class="label">Nama Produk</td>
        <td><input id="nama_inventori" /></td>
    </tr>
    <tr>
        <td class="label">Stock</td>
        <td><input id="banyak" /><input id="update_stock" name="update_stock" type="hidden"/></td>
    </tr>
    <tr>
    	<td class="label">Harga Statuan</td>
    	<td><input id="harga" /></td>
    </tr>
    <tr>
    	<td class="label">Jumlah</td>
    	<td><input id="jumlah" /><input id="jum_awal" name="jum_awal" type="hidden"/><input id="jum_akhir" name="jum_akhir" type="hidden"/></td>
    </tr>
    <tr>
    	<td class="label">Harga Bayar</td>
    	<td><input id="total_bayar" /></td>
    </tr>
    </table>
	</form>
</div>